﻿
// DAOTestDoc.cpp: CDAOTestDoc 类的实现
//

#include "pch.h"
#include "framework.h"
// SHARED_HANDLERS 可以在实现预览、缩略图和搜索筛选器句柄的
// ATL 项目中进行定义，并允许与该项目共享文档代码。
#ifndef SHARED_HANDLERS
#include "DAOTest.h"
#endif
#include "DAOTestDoc.h"
#include <propkey.h>
#include "Mainfrm.h"
#include "TableSelectDlg.h"
#include "IsamSelectDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CDAOTestDoc

IMPLEMENT_DYNCREATE(CDAOTestDoc, CDocument)

BEGIN_MESSAGE_MAP(CDAOTestDoc, CDocument)
	ON_COMMAND(ID_FILE_DAOOPEN_MDB, &CDAOTestDoc::OnFileDaoopenMdb)
	ON_COMMAND(ID_FILE_DAOOPEN_ISAM, &CDAOTestDoc::OnFileDaoopenIsam)
	ON_COMMAND(ID_FILE_DAOOPEN_ODBC, &CDAOTestDoc::OnFileDaoopenOdbc)
	ON_COMMAND(ID_FILE_DAODISCONNECT, &CDAOTestDoc::OnFileDaodisconnect)
	ON_COMMAND(IDC_BUTTON_QUERY, &CDAOTestDoc::OnReQuery)
	ON_UPDATE_COMMAND_UI(IDC_BUTTON_QUERY, &CDAOTestDoc::OnUpdateQuery)
	ON_UPDATE_COMMAND_UI(ID_FILE_DAODISCONNECT, &CDAOTestDoc::OnUpdateFileDaodisconnect)
END_MESSAGE_MAP()


void DaoErrorMsg(CDaoException* e)
{
	LPWSTR errorMsg;
	wsprintf(errorMsg, _T("DAO error %d, SOURCE = %s, DESCR = %s"),
		e->m_pErrorInfo->m_lErrorCode,
		e->m_pErrorInfo->m_strSource,
		e->m_pErrorInfo->m_strDescription);
	AfxMessageBox(errorMsg);
}

// CDAOTestDoc 构造/析构

CDAOTestDoc::CDAOTestDoc() noexcept
{
	// TODO: 在此添加一次性构造代码
	m_pRecordset = NULL;
	m_bConnected = FALSE;
	m_nFileds = 0;
	m_nRowCount = 0;
	m_DatabaseType = UNK;
}

CDAOTestDoc::~CDAOTestDoc()
{
}

BOOL CDAOTestDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: 在此添加重新初始化代码
	// (SDI 文档将重用该文档)
	m_strDatabase.Empty();
	m_strConnect.Empty();
	m_strQuery.Empty();

	return TRUE;
}




// CDAOTestDoc 序列化

void CDAOTestDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: 在此添加存储代码
		ar << (int)m_DatabaseType << m_strDatabase << m_strConnect << m_strQuery ;
	}
	else
	{
		// TODO: 在此添加加载代码
		ar >> (int&)m_DatabaseType >> m_strDatabase >> m_strConnect >> m_strQuery;
	}
}

#ifdef SHARED_HANDLERS

// 缩略图的支持
void CDAOTestDoc::OnDrawThumbnail(CDC& dc, LPRECT lprcBounds)
{
	// 修改此代码以绘制文档数据
	dc.FillSolidRect(lprcBounds, RGB(255, 255, 255));

	CString strText = _T("TODO: implement thumbnail drawing here");
	LOGFONT lf;

	CFont* pDefaultGUIFont = CFont::FromHandle((HFONT) GetStockObject(DEFAULT_GUI_FONT));
	pDefaultGUIFont->GetLogFont(&lf);
	lf.lfHeight = 36;

	CFont fontDraw;
	fontDraw.CreateFontIndirect(&lf);

	CFont* pOldFont = dc.SelectObject(&fontDraw);
	dc.DrawText(strText, lprcBounds, DT_CENTER | DT_WORDBREAK);
	dc.SelectObject(pOldFont);
}

// 搜索处理程序的支持
void CDAOTestDoc::InitializeSearchContent()
{
	CString strSearchContent;
	// 从文档数据设置搜索内容。
	// 内容部分应由“;”分隔

	// 例如:     strSearchContent = _T("point;rectangle;circle;ole object;")；
	SetSearchContent(strSearchContent);
}

void CDAOTestDoc::SetSearchContent(const CString& value)
{
	if (value.IsEmpty())
	{
		RemoveChunk(PKEY_Search_Contents.fmtid, PKEY_Search_Contents.pid);
	}
	else
	{
		CMFCFilterChunkValueImpl *pChunk = nullptr;
		ATLTRY(pChunk = new CMFCFilterChunkValueImpl);
		if (pChunk != nullptr)
		{
			pChunk->SetTextValue(PKEY_Search_Contents, value, CHUNK_TEXT);
			SetChunkValue(pChunk);
		}
	}
}

#endif // SHARED_HANDLERS

// CDAOTestDoc 诊断

#ifdef _DEBUG
void CDAOTestDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CDAOTestDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG


// CDAOTestDoc 命令

BOOL CDAOTestDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
	if (!CDocument::OnOpenDocument(lpszPathName))
		return FALSE;

	PutQuery();
	switch (m_DatabaseType)
	{
	case UNK:
		break;
	case MDB:
		DaoOpenMDB();
		break;
	case ISAM:
		DaoOpenIsam();
		break;
	case ODBC:
		DaoOpenOdbc();
		break;
	}
}

void CDAOTestDoc::OnCloseDocument()
{
	m_strQuery.Empty();
	PutQuery();
	if (m_bConnected)
	{
		delete m_pRecordset;
		m_Database.Close();
		m_bConnected = FALSE;
		m_pRecordset = NULL;
		m_nRowCount = 0;
	}
	CDocument::OnCloseDocument();
}

void CDAOTestDoc::GetQuery()
{
	wchar_t strText[201];
	CDialogBar& rBar = ((CMainFrame*)(AfxGetApp()->m_pMainWnd))->m_wndQueryBar;
	rBar.GetDlgItemTextW(IDC_EDIT_QUERY, strText,200);
	m_strQuery = strText;
}

void CDAOTestDoc::PutQuery()
{
	CDialogBar& rBar = ((CMainFrame*)(AfxGetApp()->m_pMainWnd))->m_wndQueryBar;
	rBar.SetDlgItemTextW(IDC_EDIT_QUERY,m_strQuery);
}

void CDAOTestDoc::GetFiledSpecs()
{
	m_nFileds = (int)m_pRecordset->GetFieldCount();
	m_arrFiledName.SetSize(m_nFileds);
	m_arrFiledSize.SetSize(m_nFileds);
	CDaoFieldInfo fi;
	for (int i=0;i< m_nFileds;i++)
	{
		m_pRecordset->GetFieldInfo(i, fi);
		m_arrFiledName[i] = fi.m_strName;
		m_arrFiledSize[i] = (short)max(min(fi.m_lSize,50), fi.m_strName.GetLength());
		TRACE(_T("Filed Name = %s\n"), fi.m_strName);
	}
}

void CDAOTestDoc::OpenRecordSet()
{
	GetQuery();
	if (m_strQuery.IsEmpty())
	{
		CTableSelectDlg tabDlg(&m_Database);
		if (tabDlg.DoModal() != IDOK )
		{
			m_Database.Close();
			return;
		}

		m_strQuery.Format(_T("select * from [%s]"), tabDlg.m_strSelection);
		PutQuery();
	}

	m_pRecordset = new CDaoRecordset(&m_Database);

	try
	{
		m_pRecordset->Open(dbOpenDynaset, m_strQuery, dbReadOnly);
	}
	catch (CDaoException* e)
	{
		::DaoErrorMsg(e);
		UpdateAllViews(NULL);
		m_bConnected = FALSE;
		e->Delete();
	}

	if (!m_pRecordset->IsBOF())
	{
		m_pRecordset->MoveLast();
	}
	m_nRowCount = m_pRecordset->GetAbsolutePosition() + 2;
	TRACE(_T("m_nRowCount = %d\n"), m_nRowCount);
	GetFiledSpecs();
	UpdateAllViews(NULL);
	m_bConnected = TRUE;
}

void CDAOTestDoc::DaoOpenMDB()
{
	if (m_strDatabase.IsEmpty())
	{
		CFileDialog dlg(TRUE,_T(".mdb"),_T("*.mdb"));
		if (dlg.DoModal() == IDCANCEL)
			return;
		m_strDatabase = dlg.GetPathName();
	}

	BeginWaitCursor();

	try
	{
		m_Database.Open(m_strDatabase,FALSE,TRUE);
	}
	catch (CDaoException* e)
	{
		::DaoErrorMsg(e);
		EndWaitCursor();
		e->Delete();
		return;
	}
	m_strDatabase = m_Database.GetName();
	TRACE(_T("database Name =%s,conncet = %s\n"),m_strDatabase,m_strConnect);
	OpenRecordSet();
	m_DatabaseType = MDB;
	EndWaitCursor();
}

void CDAOTestDoc::DaoOpenOdbc()
{
	if (m_strConnect.IsEmpty())
	{
		m_strConnect = _T("ODBC");
	}

	BeginWaitCursor();

	try
	{
		m_Database.Open(_T(""), FALSE, TRUE,m_strConnect);
	}
	catch (CDaoException* e)
	{
		::DaoErrorMsg(e);
		EndWaitCursor();
		e->Delete();
		return;
	}
	m_strDatabase = m_Database.GetName();
	TRACE(_T("database Name =%s,conncet = %s\n"), m_strDatabase, m_strConnect);
	OpenRecordSet();
	m_DatabaseType = ODBC;
	EndWaitCursor();
}

void CDAOTestDoc::DaoOpenIsam()
{
	if (m_strDatabase.IsEmpty())
	{
		CIsamSelectDlg dlg;
		if (dlg.DoModal() != IDOK)
			return;
		m_strConnect = dlg.m_strIsam + _T(";DATABASE = ") + dlg.m_strDirectory;
		TRACE(_T("m_strConnect = %s\n"), m_strConnect);

	}

	try
	{
		m_Database.Open(_T(""), FALSE, TRUE, m_strConnect);
	}
	catch (CDaoException* e)
	{
		::DaoErrorMsg(e);
		EndWaitCursor();
		e->Delete();
		return;
	}
	m_strDatabase = m_Database.GetName();
	TRACE(_T("database Name =%s,conncet = %s\n"), m_strDatabase, m_strConnect);
	OpenRecordSet();
	m_DatabaseType = ODBC;
	EndWaitCursor();

}


void CDAOTestDoc::OnFileDaoopenMdb()
{
	// TODO: 在此添加命令处理程序代码
	m_strDatabase.Empty();
	m_strQuery.Empty();
	DaoOpenMDB();
}


void CDAOTestDoc::OnFileDaoopenIsam()
{
	// TODO: 在此添加命令处理程序代码
	m_strDatabase.Empty();
	m_strQuery.Empty();
	DaoOpenIsam();
}


void CDAOTestDoc::OnFileDaoopenOdbc()
{
	// TODO: 在此添加命令处理程序代码
	m_strDatabase.Empty();
	m_strQuery.Empty();
	DaoOpenOdbc();
}


void CDAOTestDoc::OnFileDaodisconnect()
{
	// TODO: 在此添加命令处理程序代码
	if (m_bConnected)
	{
		delete m_pRecordset;
		m_Database.Close();
		m_bConnected = FALSE;
		m_nRowCount = 0;
		UpdateAllViews(NULL);
	}
}

void CDAOTestDoc::OnReQuery()
{
	GetQuery();
	BeginWaitCursor();
	if (m_pRecordset->IsOpen())
	{
		m_pRecordset->Close();
	}

	try
	{
		m_pRecordset->Open(dbOpenDynaset, m_strQuery, dbReadOnly);
	}
	catch (CDaoException* e)
	{
		::DaoErrorMsg(e);
		m_nRowCount = 0;
		UpdateAllViews(NULL);
		EndWaitCursor();
		e->Delete();
		return;
	}

	if (!m_pRecordset->IsBOF())
	{
		m_pRecordset->MoveLast();
	}

	m_nRowCount = m_pRecordset->GetAbsolutePosition() + 2;
	TRACE(_T("m_nRowCount = %d\n"), m_nRowCount);
	GetFiledSpecs();
	UpdateAllViews(NULL);
	EndWaitCursor();
}

void CDAOTestDoc::OnUpdateFileDaodisconnect(CCmdUI* pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
	pCmdUI->Enable(m_bConnected);
}

void CDAOTestDoc::OnUpdateQuery(CCmdUI* pCmdUI)
{
	pCmdUI->Enable(m_bConnected);
}
